<template>
  <div>
    <el-drawer
      title="消费记录"
      :before-close="handleClose"
      :visible.sync="orderRecordDialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      size="60%"
    >
      <div class="demo-drawer__content">
        <el-form ref="rechargeDetailRef" :inline="true" :model="queryParams">
          <el-row>
            <el-col :span="6">
              <el-form-item label="油品" prop="oilName">
                <el-select
                  v-model="queryParams.oilName"
                  placeholder="请选择"
                  size="small"
                  style="width: 150px"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in oilList"
                    :key="index"
                    :label="item.oilName"
                    :value="item.oilId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="消费时间">
                <el-date-picker
                  v-model="dateTimeRange"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  size="small"
                  style="width: 330px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="getList"
                  >搜索</el-button
                >
                <el-button
                  type="primary"
                  icon="el-icon-refresh"
                  plain
                  size="mini"
                  @click="resetQuery"
                  >重置</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table stripe :data="consumeList" v-loading="loading">
          <el-table-column
            type="index"
            label="序号"
            width="50"
            align="left"
          ></el-table-column>
          <el-table-column
            prop="orderNo"
            label="订单号"
            width="180"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="customerName"
            label="消费人"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="oilName"
            label="油品"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="amt"
            label="消费金额"
            sortable
            show-overflow-tooltip
            width="120"
          ></el-table-column>
          <el-table-column
            prop="createdDate"
            label="消费时间"
            width="180"
            sortable
            show-overflow-tooltip
          ></el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <div style="width: 100%; text-align: end; margin-top: 30px">
        <el-button @click="handleClose" type="primary" size="small"
          >返回</el-button
        >
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { getListByEnterprise } from "@/api/system/dept";
import { getSonCardConsumeRecordListApi ,getList } from "@/api/teamCard/sonCard";
export default {
  name: "OrderRecord",
  props: {
    orderRecordDialog: {
      type: Boolean,
      default: false,
    },
    currentData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      total: 0,
      dateTimeRange: [],
      consumeList: [],
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payType: "cdk", // 支付类型： cdk 车队卡
        cdkType: "fleet_card_sub", // 车队卡的类型    fleet_card 主卡  fleet_card_sub 子卡
      },
      oilList: [],
    };
  },
  watch: {
    orderRecordDialog(val) {
      if (val) {
        this.getList();
        this.getOilListData();
      }
    },
  },
  methods: {
    handleClose() {
      this.$emit("update:orderRecordDialog", false);
    },
    // 重置
    resetQuery() {
      this.resetForm("rechargeDetailRef");
      this.dateTimeRange = [];
      this.getList();
    },
    // 获取消费记录列表
    getList() {
      this.loading = true;
      const id = this.currentData.id;
      const params = {
        ...this.queryParams,
        consumerId: id,
      };
      if (this.dateTimeRange && this.dateTimeRange.length) {
        params.beginTime = this.formatterTime(this.dateTimeRange[0]);
        params.endTime = this.formatterTime(this.dateTimeRange[1]);
      }
      getList(params).then((res) => {
        this.consumeList = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    formatterTime(time){
      let date = new Date(time);
      let year = date.getFullYear();
      let month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
      let day = String(date.getDate()).padStart(2, '0');
      let hours = String(date.getHours()).padStart(2, '0');
      let minutes = String(date.getMinutes()).padStart(2, '0');
      let seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    // 获取油品
    async getOilListData() {
      const res = await getListByEnterprise();
      this.oilList = res.data;
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .demo-drawer {
  overflow: auto;
  .el-drawer__header {
    border-bottom: 1px solid #990;
    padding-bottom: 20px;
  }
  .el-drawer__body {
    padding: 20px;
  }
}
</style>
